package com.qf.ssm.service.impl;
/**
   @author 86155
   @ClassName: com.qf.ssm.service.impl.StoreServiceImpl
   @Description: 该类的功能描述
   @date: 2021/10/11 19:56 
*/

import com.github.pagehelper.PageHelper;
import com.qf.ssm.common.exception.ServiceException;
import com.qf.ssm.common.vo.ResponseCode;
import com.qf.ssm.entity.Evaluate;
import com.qf.ssm.entity.Product;
import com.qf.ssm.entity.Store;
import com.qf.ssm.entity.Type;
import com.qf.ssm.mapper.StoreAppMapper;
import com.qf.ssm.service.StoreAppService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StoreAppServiceImpl implements StoreAppService {
    @Resource
    private StoreAppMapper storeAppMapper;

    /**
     * 展示所有的店铺经营类型
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<Type> findAllType(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Type> types = storeAppMapper.selectAllType();
        return types;
    }

    /**
     * 分页查询，点击首页的10个类型，根据tid去查找对应类型的商店
     *
     * @param tid  商店id
     * @param page 当前页
     * @param size 每页总数
     * @return
     */
    @Override
    public List<Store> findStoreByTid(Long tid, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Store> stores;
        try {
            stores = storeAppMapper.selectStoreByTid(tid);
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return stores;
    }

    /**
     * 查找店铺的详情信息，包括全部商品
     *
     * @param sid
     * @return
     */
    @Override
    public Store findStoreBySid(Long sid) {
        Store store = null;
        try {
            store = storeAppMapper.selectStoreBySid(sid);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return store;
    }

    /**
     * 根据店铺名称或者商品名称进行查询,先根据商店名称查询，查询不到再查找商品
     * @param searchName
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<?>searchStoreOrProduct(String searchName, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Store> stores = storeAppMapper.searchStoreByName(searchName);
        if (stores == null || stores.size()==0) {
            List<Product> products = storeAppMapper.searchProductByName(searchName);
            return products;
        }
        return stores;
    }

    @Override
    public List<Evaluate> findEvaluate(Long storeId) {
        List<Evaluate> evaluateList = null;
        try {
            evaluateList = storeAppMapper.selectEvaluate(storeId);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return evaluateList;
    }
}
